//
//  YXYAttentionViewController.m
//  Topit
//
//  Created by dllo on 15/12/12.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYAttentionViewController.h"
#import "DataBaseHandle.h"
#import "YXYSelectionViewController.h"
@interface YXYAttentionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation YXYAttentionViewController

-(void)dealloc {
    [_tableView release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.array = [NSMutableArray arrayWithCapacity:0];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"收藏";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissViewController:)];
    [self setUpTableView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DataBaseHandle *dataBaseHandle = [DataBaseHandle shareDataBase];
    [dataBaseHandle openDataBase];
    self.array = [dataBaseHandle selectDataBase];
    [dataBaseHandle closedDataBase];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];
    if ([passWord isEqualToString:@"Night"]) {
        self.tableView.backgroundColor = [UIColor blackColor];
    }else {
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    [self.tableView reloadData];
}

#pragma mark - tableView 
- (void)setUpTableView {

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}

#pragma mark - tableViewDataSource 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];
  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
     NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.array[indexPath.row]];
    cell.textLabel.text = [dic valueForKey:@"title"];
    [cell.textLabel nightMode];
    if ([passWord isEqualToString:@"Night"]) {
        cell.backgroundColor = [UIColor colorWithHue:34 / 255.0 saturation:34 / 255.0 brightness:34 / 255.0 alpha:1];
    }else {
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark - 点击收藏跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.array[indexPath.row]];
    YXYSelectionViewController *selectionVC = [[YXYSelectionViewController alloc] initWithURL:[dic valueForKey:@"URL"]];
    [self.navigationController pushViewController:selectionVC animated:YES];
    [selectionVC release];
}

//控制是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //老王小贴士：先处理数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.array[indexPath.row]];
        [self.array removeObjectAtIndex:indexPath.row];
        DataBaseHandle *databaseHandle = [DataBaseHandle shareDataBase];
        [databaseHandle openDataBase];
        [databaseHandle deleteDataBase:[dic valueForKey:@"title"]];
        [databaseHandle closedDataBase];
        //        再刷新tableView
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

- (void)dismissViewController:(UIBarButtonItem *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
